<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:auth=""
>
<auth:data uri="/upload/image"/>
<head>
    <th:block th:include="common/head::noXadmin('上传文件')"/>
    <style>
        .layui-upload-list {
            height: 200px;
            text-align: center;
            margin: 0 auto;
            padding: 40px;
        }

        .layui-upload-list img {
            width: 200px;
            height: 200px;
            background: #efefef;
            border: 1px solid black;
        }
    </style>
</head>
<body style="padding: 20px 10px;">


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>图片上传</legend>
</fieldset>

<blockquote class="layui-elem-quote ">
    支持图片类型：jpg,gif,png,bmp,png<br/>
    支持业务类型：<span th:each="bt:${T(com.hardydou.framework.filestore.AbstractFileStore.BusType).values()}"
                 th:text="${bt.name()}" style="padding: 10px;">
</blockquote>

<div class="layui-upload text-align-center">
    <div class="layui-upload-list">
        <img class="layui-upload-img" id="demo1">
        <img class="layui-upload-img" id="demo2">
        <p id="demoText" style="white-space: pre;">本地图片 服务器图片</p>
    </div>
    <button type="button" class="layui-btn" id="test1">上传图片</button>
</div>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>表格上传</legend>
</fieldset>

<script>
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/upload/image' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 0) {
                    console.log(res);
                    return layer.msg('上传失败' + res.code);
                }
                //上传成功
                $('#demo2').attr('src', res.data.url); //图片链接（base64）
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>

</html>
